package com.kcht.itsed.mws.aleframework.biz;

import com.kcht.itsed.mws.aleframework.intf.ITagMemoryContainer;
import com.kcht.itsed.mws.alemodel.TMFixedFieldListSpec;
import com.kcht.itsed.mws.alemodel.TMFixedFieldSpec;
import com.kcht.itsed.mws.alemodel.TMSpec;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;


@Component
public class TagMemoryContainer implements ITagMemoryContainer{

	private Map<String,TMSpec> tmSpecContainer = new HashMap<>();
	private Map<String,TMFixedFieldSpec> fixedFieldSpecContainer = new HashMap<>();
	
	
	@Override
	public synchronized void updateTMSpec(String tmSpecName, TMSpec tmSpec) {
		tmSpecContainer.put(tmSpecName,tmSpec);
		
		if(tmSpec instanceof TMFixedFieldListSpec){
			((TMFixedFieldListSpec)tmSpec).getFixedFields().getFixedField()
				.forEach(field->fixedFieldSpecContainer.put(field.getFieldname(),field));
		}
		//暂时只处理TMFixedFieldListSpec的类型，可变规则不处理
	}

	@Override
	public synchronized void removeTMSpec(String tmSpecName) {
		TMSpec tmSpec = tmSpecContainer.remove(tmSpecName);
		if(tmSpec instanceof TMFixedFieldListSpec){
			((TMFixedFieldListSpec)tmSpec).getFixedFields().getFixedField()
				.forEach(field->fixedFieldSpecContainer.remove(field.getFieldname()));
		}
	}

	@Override
	public synchronized TMFixedFieldSpec getFixedFieldDefination(String fieldName) {
		return fixedFieldSpecContainer.get(fieldName);
	};
}
